<?php
class FileController extends ManagerController {
	function FileController(){
		parent::ManagerController();
	}

	/**
	 * 首页，目录管理(分类)
	 */
	public function index(){
		$file_mod = new FileModel();
		self::init_page_info();
		$condition = array('_id'=>SITE_ID);
		$files = $file_mod->find($condition, null, $this->page_info->get_start(), $this->page_info->page_size);
		self::set_page_total($file_mod->count($condition));
		self::assign('files', $files);
		
	
		//获取当前图片分类
		$file_cate_mod = new File_cateModel();
		$image_cates = $file_cate_mod->get_cates('image');
		if(!empty($image_cates)){
			$tree = new Tree($image_cates);
			$this->assign('image_cates', $tree->get_formated_list('　'));
		}
		
		$video_cates = $file_cate_mod->get_cates('video');
		if(!empty($video_cates)){
			$tree = new Tree($video_cates);
			$this->assign('video_cates', $tree->get_formated_list('　'));
		}
		
		$this->display();
	}
	
	public function files(){
		$type = self::get_val('type', 'image');
		
		$condition = array('site_id'=>SITE_ID, 'type'=>FileModel::get_type_by_name($type));
		if(isset($_GET['cate'])){
			//指定了分类
			$condition['cate'] = trim($_GET['cate']);
		}
		self::init_page_info(21);
		$file_mod = new FileModel();
		$files = $file_mod->find($condition,null, $this->page_info->get_start(), $this->page_info->page_size);
		self::set_page_total($file_mod->count($condition));
		
		$file_cate_mod = new File_cateModel();
		$image_cates = $file_cate_mod->get_cates($type);
		if(!empty($image_cates)){
			$tree = new Tree($image_cates);
			$this->assign('cates', $tree->get_options('　'));
		}
		foreach($files as &$file){
			if(isset($file['cate']) && isset($image_cates[$file['cate']])){
				$file['cate_name'] = $image_cates[$file['cate']]['name'];
			}else{
				$file['cate_name'] = '未分类';
			}
		}
		$types = FileModel::get_types();
		self::assign('type_name', $types[$type]);
		self::assign('files', $files);
		$this->display();
	}
	
	public function cate(){
		$cate_id = self::get_request_val('cate', 'image');
		if(IS_POST){
			self::save_cate($cate_id);
		}
		$cate_names = array('image'=>'图片','video'=>'视频');
		//获取当前图片分类
		$file_cate_mod = new File_cateModel();
		$file_cates = $file_cate_mod->get_cates($cate_id);
		if(!empty($file_cates)){
			$tree = new Tree($file_cates);
			$this->assign('cates', $tree->get_formated_list());
			$this->assign('options', $tree->get_options('　'));
		}
		$this->assign('cate_id', $cate_id);
		$this->assign('cate_name', $cate_names[$cate_id]);
		$this->display();
	}
	
	private function save_cate($cate_name){
		$name = self::get_post_val('name');
		$parent = self::get_post_val('parent');
		//验证
		if(empty($name) || utf8_strlen($name)<1 || utf8_strlen($name)>50){
			self::ajax_error('分类名称长度应该在1至50之间！');
		}
		$cate = array(
			'name' => $name,
		);
		if(!empty($parent)){
			$cate['parent'] = $parent;
		}
		$image_cate_mod = new File_cateModel();
		if(empty($this->id)){
			//新增
			$id = gen_guid();
			if($image_cate_mod->exists_key(SITE_ID)){
				$image_cate_mod->update_by_key(SITE_ID, array($cate_name . '.' . $id => $cate));
			}else{
				$image_cate_mod->insert(
					array(
						'_id' => SITE_ID,
						$cate_name => array(
							$id => $cate,
						)
					)
				);
			}
			self::ajax_info('新增成功！');
		}else{
			//修改
			$image_cate_mod->update_by_key(SITE_ID, array($cate_name . '.' . $this->id => $cate));
			self::ajax_info('修改成功！');
		}
	}
	
	public function move(){
		$file = self::get_post_val('file');
		$cate = self::get_post_val('cate');
		//设置
		$file_mode = new FileModel();
		$file_mode->update_by_key($file, array('cate'=>$cate));
		echo json_encode(array('result'=>true, 'info'=>'操作成功！'));
	}
	
	public function del(){
		if(empty($this->id)){
			self::ajax_error('未指定要删除的文件！');
		}
		$file_mod = new FileModel();
		$file_mod->delete_by_key($this->id);
		self::ajax_info('删除成功！');
	}
	
	public function cate_del(){
		if(empty($this->id)){
			self::ajax_error('未指定要删除的分类！');
		}
		$cate_id = self::get_request_val('cate', 'image');
		$file_cate_mod = new File_cateModel();
		$file_cate_mod->delete_column_by_key(SITE_ID, $cate_id . '.' .$this->id);
		self::ajax_info('删除成功！');
	}
	
	public function template(){
		$condition = array('site_id'=>SITE_ID);
		$page_mod = new PageModel();
		self::init_page_info();
		$total = $page_mod->count($condition);
		self::set_page_total($total);
		if($total>0){
			$types = PageModel::get_page_types();
			$pages = $page_mod->find($condition, 'name,update,desc,available,agent', $this->page_info->get_start(), $this->page_info->page_size);
			foreach ($pages as &$page){
				if(isset($types[$page['name']])){
					$page['page_name'] = $types[$page['name']]['name'];
					$page['url'] = $types[$page['name']]['url'];
				}
			}
			$this->assign('pages', $pages);
		}
		$this->display();
	}
	
	public function template_del(){
		//删除某个模板
		if(!isset($this->id)){
			self::ajax_error('未指定要删除的模板！');
		}
		$page_mod = new PageModel();
		$page = $page_mod->get_by_key_site_limit($this->id, 'available,name,agent');
		$page_name = $page['name'];
		$agent = $page['agent'];
		if(empty($page)){
			self::ajax_error('要删除的模板不存在或者已被删除！');
		}
		if($page['available'] === true){
			self::ajax_error('正在使用中的模板不能删除！');
		}
		//删除操作
		$page_mod->delete_by_key_site_limit($this->id);
		//删除文件
		unlink(USER_TEMPLATE_PATH . $agent . '/css/' . $page_name . '_' . $this->id . '.css');
		unlink(USER_TEMPLATE_PATH . $agent . '/' . $page_name . '_' . $this->id . '.' . TEMPLATE_TYPE);
		self::ajax_info('删除成功！');
	}
	
	public function template_edit(){
		if(IS_POST){
			self::template_add();
		}
		$types = PageModel::get_page_types();
		
		$agents = array(
			'web' => 'Web',
			'wap' => 'Wap',
			'pad' => 'PAD'
		);
		
		$this->assign('agents', $agents);
		$this->assign('types', $types);
		$this->display();
	}
	
	private function template_add(){
		$type = self::get_post_val('type');
		$desc = self::get_post_val('desc');
		$agent = self::get_post_val('agent');
		$tree = new PageTree();
		$tree->create($type, $agent, $desc);
		$tree->save();
		self::ajax_info('保存成功！');
	}
	
	/**
	 * 发布某个页面，将作以下操作：
	 * 1. 判断这页面是否有已经发布的，如果有，则将其设置为非发布并重命名
	 * 2. 将此页面设置为已发布，并重命名模板
	 */
	public function template_public(){
		$page_mod = new PageModel();
		if(empty($this->id)){
			self::ajax_error('未指定要发布的页面！');
		}
		//获取当前要发布的页面
		$page = $page_mod->get_by_key_site_limit($this->id, 'agent,name,available');
		if(empty($page)){
			self::ajax_error('未找到要发布的页面！');
		}
		
		//查找原来发布的版本
		$old_page = $page_mod->get_by_attrs(array('site_id'=>SITE_ID, 'name'=>$page['name'], 'agent'=>$page['agent'], 'available'=>true));
		if(!empty($old_page)){
			//重命名旧文件
			$agent_dir = USER_TEMPLATE_PATH . $old_page['agent'];
			rename($agent_dir . '/' . $old_page['name'] . '.' . TEMPLATE_TYPE, $agent_dir . '/' . $old_page['name'] . '_' . $old_page['_id'] . '.' . TEMPLATE_TYPE);
		}
		//重命名旧文件为应用
		$agent_dir = USER_TEMPLATE_PATH . $page['agent'];
		rename($agent_dir . '/' . $page['name'] . '_' . $page['_id'] . '.' . TEMPLATE_TYPE, $agent_dir . '/' . $page['name'] . '.' . TEMPLATE_TYPE);
		//更新数据库
		$page_mod->update_by_key_site_limit($old_page['_id'], array('available'=>false));
		$page_mod->update_by_key_site_limit($this->id, array('available'=>true));
		self::ajax_info('发布成功！');
	}
}

?>